package com.atguigu.tingshu.account.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.vo.account.RechargeInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@SuppressWarnings({"all"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

    @Autowired
    private RechargeInfoMapper rechargeInfoMapper;

    /**
     * 保存充值记录
     *
     * @param rechargeInfo
     * @return
     */
    @Override
    public Map<String, String> submitRecharge(RechargeInfoVo rechargeInfoVo) {
        String czOrderNo = "cz" + DateUtil.today().replaceAll("-", "") + IdUtil.getSnowflakeNextId();
        RechargeInfo rechargeInfo = new RechargeInfo();
        rechargeInfo.setOrderNo(czOrderNo);
        rechargeInfo.setUserId(AuthContextHolder.getUserId());
        rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
        rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
        rechargeInfo.setPayWay(rechargeInfoVo.getPayWay());
        rechargeInfoMapper.insert(rechargeInfo);

        Map<String, String> map = new HashMap<>();
        map.put("orderNo", czOrderNo);
        return map;
    }

    @Autowired
    private UserAccountMapper userAccountMapper;

    @Autowired
    private UserAccountService userAccountService;

    /**
     * 用户微信付款成功后，处理充值成功业务
     *
     * @param orderNo
     * @return
     */
    @Override
    public void rechargePaySuccess(String orderNo) {
        //1.根据订单编号查询充值记录，修改充值状态
        LambdaQueryWrapper<RechargeInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RechargeInfo::getOrderNo, orderNo);
        RechargeInfo rechargeInfo = this.getOne(queryWrapper);
        Assert.notNull(rechargeInfo, "充值记录不存在");
        String rechargeStatus = rechargeInfo.getRechargeStatus();
        if (!rechargeStatus.equals(SystemConstant.ORDER_STATUS_PAID)) {
            //只要是不为“已支付”就处理
            rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_PAID);
            rechargeInfoMapper.updateById(rechargeInfo);
            //2.完成余额充值
            userAccountMapper.add(rechargeInfo.getUserId(), rechargeInfo.getRechargeAmount());
            //3.新增账户变动日志
            userAccountService.saveUserAccountDetail(rechargeInfo.getUserId(), "充值", SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT, rechargeInfo.getRechargeAmount(), orderNo);
        }
    }
}
